package com.example.speeddating.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.annotaion.LoginAdmin;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.entity.*;
import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.service.ArticleService;
import com.example.speeddating.service.ReportService;
import com.example.speeddating.service.ReportTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:34
 */
@RestController
public class ReportController {

    @Autowired
    private ReportService reportService;

    @Autowired
    private ReportTypeService reportTypeService;

    @Autowired
    private ArticleService articleService;

    /**
     * 查询所有举报类型
     * @return
     */
    @RequestMapping("/report/all")
    public BaseResponse<List<ReportType>> AllReportType(){
        List<ReportType> list = reportTypeService.list();
        return RespGenerator.returnOK(list);
    }

    /**
     * 举报用户
     * @param tokenEntity
     * @param jsonString
     * @return
     */
    @PostMapping("/user/report")
    @TokenCheck(roles = "user")
    public BaseResponse reportUser(@LoginUser TokenEntity tokenEntity,@RequestBody HashMap<String,Object> jsonString){
        Report report = new Report();
        report.setUserId(tokenEntity.getId());
        report.setArticleId(Long.valueOf(jsonString.get("articleId").toString()));
        report.setTypeId(Integer.valueOf(jsonString.get("typeId").toString()));
        boolean save = reportService.save(report);
        return RespGenerator.returnOK(save);
    }

    @GetMapping("/admin/report/all")
    @TokenCheck(roles = "admin")
    public BaseResponse reportUser(@LoginAdmin Admin admin,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum , @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        Page<Report> reportPage = reportService.selectAllReport(pageNum,pageSize);
        for (Report record : reportPage.getRecords()) {
            Article article = record.getArticle();
           if (article==null)continue;
            ArrayList<String> articleImage = articleService.getArticleImage(article.getId());
            if (articleImage==null){
                article.setImages(new ArrayList<>());
            }else {
                article.setImages(articleImage);
            }
        }
        return RespGenerator.returnOK(reportPage);
    }

    /**
     * 处理
     * @param admin
     * @param id
     * @return
     */
    @PutMapping("/admin/report/handle/{id}")
    @TokenCheck(roles = "admin")
    public BaseResponse handleReportUser(@LoginAdmin Admin admin,@PathVariable Long id){
        if (id==null){
            throw new CommonException("请求参数错误");
        }
        reportService.handleFeedback(id);
        return RespGenerator.returnOK("修改成功");
    }

    /**
     * 删除举报信息
     * @param admin
     * @return
     */
    @TokenCheck(roles = "admin")
    @DeleteMapping("/admin/report/delete/{id}")
    public BaseResponse<Page> deleteFeedback(@LoginUser Admin admin,@PathVariable Long id){
        if (id==null){
            throw new CommonException("请求参数错误");
        }
        reportService.deleteFeedback(id);
        return RespGenerator.returnOK("删除成功");
    }


}
